<?php
namespace app\admin\controller;
use app\common\model\Article as ArticleModel;
use app\common\controller\AdminBase;

/**
 * 文章管理
 * Class Article
 * @package app\admin\controller
 */
class Article extends AdminBase
{
    protected $article_model;
	protected $category_list=[
        ['id' => 1,'name' => '公告'],
        ['id' => 2,'name' => '福利']
	];

    protected function initialize()
    {
        parent::initialize();
        $this->article_model  = new ArticleModel();
		$this->assign('category_list',$this->category_list);
    }

    /**
     * 文章管理
     * @param int    $type     分类ID
     * @param string $keyword 关键词
     * @param int    $page
     * @return mixed
     */
    public function index()
    {
        $param = $this->request->param();
        $page = $param['page'] ?? 1;
        $map   = [];
        $field = '*';
        if (!empty($param['keyword'])) {
            $map[] = ['title','like', "%{$param['keyword']}%"];
        }
        if (!empty($param['category']) && $param['category'] > 0) {
            $map[] = ['category','=', $param['category']];
        }
        $map[] = ['status','>',-1];

        $article_list  = $this->article_model->field($field)
        ->where($map)
        ->order(['sort' => 'DESC'])
        ->paginate(100, false, ['page' => $page]);
        return $this->fetch('index', ['article_list' => $article_list, 'category_list' => $this->category_list]);
    }

    /**
     * 添加文章
     * @return mixed
     */
    public function add()
    {
        return $this->fetch();
    }
    

    /**
     * 保存文章
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            $validate_result = $this->validate($data, 'Article');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if ($this->article_model->allowField(true)->save($data)) {
                    $this->success('保存成功', url('admin/Article/index'));
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }

    /**
     * 编辑文章
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $article = $this->article_model->find($id);

        return $this->fetch('edit', ['article' => $article]);
    }

    /**
     * 更新文章
     * @param $id
     */
    public function update($id)
    {
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            $validate_result = $this->validate($data, 'Article');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if ($this->article_model->allowField(true)->save($data, $id) !== false) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }
    }


    /**
     * 更新文章
     * @param $id
     */
    public function check()
    {
        if ($this->request->isPost()) {
            $data=$this->request->param();
            if ($this->article_model->update($data) !== false) {
                $this->success('发布成功');
            } else {
                $this->error('发布失败');
            }
        }
    }

    /**
     * 删除文章
     * @param int   $id
     * @param array $ids
     */
    public function delete($id = 0)
    {
        $ids = $this->request->post();
        if(!empty($ids) && is_array($ids)){
            $id = implode(',',$ids['id']);
        }
        if ($id) {
            if ($this->article_model->whereIn('id',$id)->update(['status'=>'-1'])) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }

        } else {
            $this->error('请选择需要删除的文章');
        }
    }

    /**
     * 文章审核状态切换
     * @param array  $ids
     * @param string $type 操作类型
     */
    public function toggle($ids = [], $type = '')
    {
        $data   = [];
        $status = $type == 'audit' ? 1 : 0;

        if (!empty($ids)) {
            foreach ($ids as $value) {
                $data[] = ['id' => $value, 'status' => $status];
            }
            if ($this->article_model->saveAll($data)) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        } else {
            $this->error('请选择需要操作的文章');
        }
    }
}